package org.pageley.games.sanctuary.domain;

public enum Direction {
	NORTHEAST(1, -1),
	SOUTHWEST(-1, 1),
	SOUTHEAST(1, 1),
	NORTHWEST(-1, -1),
	NORTH(0, -1),
	SOUTH(0, 1),
	EAST(1, 0),
	WEST(-1, 0);

	 private int x;
	 private int y;
	 
	 private Direction(int x, int y) {
		 this.x = x;
		 this.y = y;
	 }
	 
	 public Location fromLocation(Location location) {
		 return new SimpleLocation(location.getX() + this.x, location.getY() + this.y);
	 }

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public static Direction directionOf(Location from, Location to) {
		return fromDelta(to.getX() - from.getX(), to.getY() - from.getY());
	}
	
	private static Direction fromDelta(int deltaX, int deltaY) {
		Direction result = null;
		for(Direction direction : values()) {
			if(direction.x == deltaX && direction.y == deltaY) {
				result = direction;
				break;
			}
		}
		return result;
	}
}
